package com.scoy.honeymei.activity;

import android.os.Bundle;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.scoy.honeymei.R;
import com.scoy.honeymei.base.BaseActivity;
import com.scoy.honeymei.url.MyUrl;
import com.scoy.honeymei.utils.MyUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: WebActivity$
 * @Description:
 * @Author: GaoZhan
 * @CreateDate: 2019/5/16/0016$ 下午 16:55$
 * @UpdateUser: 更新者
 */
public class WebActivity extends BaseActivity {


    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.sign_tv)
    TextView signTv;
    @BindView(R.id.title_llt)
    LinearLayout titleLlt;
    @BindView(R.id.custom_web)
    WebView wv;
    private WebActivity mContext;
    private int webType;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.custom_activity_web);
        ButterKnife.bind(this);
        mContext = WebActivity.this;
        webType = getIntent().getIntExtra("webType", 0);
        String title = getIntent().getStringExtra("title");
        String url = getIntent().getStringExtra("url");

        if (webType != -1) {
            getTrans();
        }else {
            titleTv.setText(title);
            initWeb(url);
        }



        initNormal();

    }

    @Override
    public void initNormal() {

        MyUtil.setStatusBar(mContext, getWindow(), true, 0);
        titleLlt.setPadding(0, MyUtil.getStatusBarHeight(mContext), 0, 0);

    }

    private void getTrans() {

        switch (webType) {
            case 1: //关于我们
                titleTv.setText("关于我们");//类型1旅游预约须知；2用户协议；3关于我们；4旅游温馨提示；5出行温馨提示；6淘票温馨提示
                initWeb(MyUrl.PREFIX_PIC + "/detail/html/weAbout.html?type=3");
                break;
            case 2://注册协议
                titleTv.setText("用户协议");
                initWeb(MyUrl.PREFIX_PIC + "/detail/html/weAbout.html?type=2");
                break;
            case 3://隐私协议
                titleTv.setText("隐私协议");
                initWeb(MyUrl.PREFIX_PIC + "/detail/html/weAbout.html?type=7");
                break;

            case 0:
                initWeb("https://www.baidu.com/");
                break;
        }


    }


    public void initWeb(String meUrl) {

        wv.loadUrl(meUrl);
        //获得WebSetting,设置js有效
        WebSettings setting = wv.getSettings();
        setting.setJavaScriptEnabled(true);
        //把WebViewHelper的对象，传递到js中，供调用
        //  wv.addJavascriptInterface(new WebViewHelper(), "wvHelper");

        setting.setAppCacheEnabled(true);//启用localstorage本地存储api
        setting.setLightTouchEnabled(true);//启用选中功能
        setting.setDomStorageEnabled(true);//启用dom存储(关键就是这句)，貌似网上twitter显示有问题也是这个属性没有设置的原因
        setting.setDatabaseEnabled(true);//启用html5数据库功能
        setting.setDisplayZoomControls(false); //隐藏webview缩放按钮
        // webSettings.setBuiltInZoomControls(true); // 显示放大缩小
        // webSettings.setSupportZoom(true); // 可以缩放

        wv.setWebChromeClient(new WebChromeClient() {
            @Override
            public boolean onJsAlert(WebView view, String url, String message,
                                     JsResult result) {
                // TODO Auto-generated method stub
                return super.onJsAlert(view, url, message, result);
            }
        });

        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });

    }


    @OnClick({R.id.back_iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back_iv:
                finish();
                break;
        }
    }
}
